package demo1;

import java.util.Random;
import java.util.Scanner;

public class Demo3 {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入矩阵的n值");
        Random random=new Random();
        int n=sc.nextInt();
        int[][] matrix=new int[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                matrix[i][j]=random.nextInt(100)+1;
            }
        }

        int sum=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                System.out.print(matrix[i][j]+"\t");
                if(i==j){
                    sum+=matrix[i][j];
                }
            }
            System.out.println();
        }
        System.out.println("对角的和为"+sum);

    }
}
